<template>
  <div class="search">
    <Form ref="form" :model="formItem" :rules="formItemRules" :label-width="100">
      <FormItem label="策略名称" prop="policyName">
        <Input v-model="formItem.policyName" placeholder="请输入内容"></Input>
      </FormItem>
      <FormItem label="策略类型" prop="policyType">
        <Select v-model="formItem.policyType">
          <Option value="url" label="接口(url)"></Option>
          <Option disabled value="origin" label="来源(origin)"></Option>
          <Option disabled value="user" label="用户(user)"></Option>
        </Select>
      </FormItem>
      <FormItem label="单位时间" prop="intervalUnit">
        <Select v-model="formItem.intervalUnit">
          <Option value="seconds" label="秒(seconds)"></Option>
          <Option value="minutes" label="分钟(minutes)"></Option>
          <Option value="hours" label="小时(hours)"></Option>
          <Option value="days" label="天(days)"></Option>
        </Select>
      </FormItem>
      <FormItem label="限流数" prop="limitQuota">
        <InputNumber :min="10" v-model="formItem.limitQuota"></InputNumber>
      </FormItem>
    </Form>
  </div>
</template>

<script>
export default {
  name: 'RateLimitInfo',
  data () {
    return {
      isAdd: true,
      formItemRules: {
        policyName: [
          { required: true, message: '策略名称不能为空', trigger: 'blur' }
        ],
        policyType: [
          { required: true, message: '策略类型不能为空', trigger: 'blur' }
        ]
      },
      formItem: this.getFormItem()
    }
  },
  methods: {
    getFormItem () {
      return {
        policyId: '',
        policyName: '',
        policyType: 'url',
        intervalUnit: 'seconds',
        limitQuota: 10
      }
    },
    setData (data) {
      this.isAdd = true
      if (data && data.policyId) {
        Object.assign(this.formItem, {
          policyId: data.policyId,
          policyName: data.policyName,
          policyType: data.policyType,
          intervalUnit: data.intervalUnit,
          limitQuota: parseInt(data.limitQuota)
        })
      }
    },
    handleReset () {
      this.$refs['form'].resetFields()
      this.formItem = this.getFormItem()
    },
    handleSubmit () {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.$http.post(this.$apis.rateLimit.save, this.formItem, this).then(res => {
              resolve(res)
            }).catch(function (err) {
              reject(err)
            })
          } else {
            resolve(false)
          }
        })
      })
    }
  },
  created () {

  },
  mounted () {
  }
}
</script>

<style scoped lang="less" rel="stylesheet/less">

</style>
